package com.idelan.commonMessage.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
/**
* @Description:    java类作用描述
* @Author:         jzhou
* @CreateDate:     2018\7\19 0019 15:41
* @UpdateUser:     jzhou
* @UpdateDate:     2018\7\19 0019 15:41
* @UpdateRemark:   修改内容
* @Version:        1.0
*/
public class RabbitmqSenderUtil {
    //发送消息队列的名字
    public static int send(String queueName,String message,String host,Integer port,String userName,String password) throws Exception {
        // 获取连接
        int result=0;
        Connection connection = RabbitmqConnectionUtil.getConnection(host,port,userName,password,null);
        // 创建通道
        Channel channel = connection.createChannel();
        // 声明队列 (不存在则创建)
        channel.queueDeclare(queueName, true, false, false, null);
        // 发送消息
        //String message = "Hello World2";
        channel.basicPublish("", queueName, null, message.getBytes());
        // 关闭通道和连接
        if (channel!=null){
            result=1;
        }
        channel.close();
        connection.close();
        return result;
    }
    public static void main(String[] args) throws Exception {
        String queueName="test111";
        String message="content!!!!!!!!!!!";
        String host="127.0.0.1";
        Integer port=5672;
        String userName="guest";
        String password="guest";
        RabbitmqSenderUtil.send(queueName,message,host,port,userName,password);
        System.out.println("send success!!!");
    }
}
